<?php
/*
	La classe modèle pour les Auteurs.
	Le constructeur permet de préparer les requêtes, la connexion avec la BD est récupérée grâce à son statut de propriété de la classe CF.
*/

include_once('./Model/Template.php');
include_once('./MyPDO.php');  

class Model_Inscrit extends Model_Template{

	private $getAncienMdp;
	private $modifMdp;
	private $connexion;
	
	public function __construct($db){
		parent::__construct($db);
		
		$inscription = 'INSERT INTO Users VALUES(:nom, :prenom, :pseudo, :mdp, :jourNaiss, :moisNaiss, :anneeNaiss, :photo)';
		$this->insert = Controller_Template::$db->prepare($inscription);
		
		$login = 'SELECT COUNT(*) FROM Users WHERE pseudo = :pseudo';
		$this->verifLog = Controller_Template::$db->prepare($login);
		
		$mdp = 'SELECT COUNT(*) FROM Users WHERE mdp = :mdp';
		$this->verifMdp = Controller_Template::$db->prepare($mdp);
		
		$mes='SELECT titre, date, Users.pseudo as expediteur, messages.id as id_message 
				FROM messages, Users 
				WHERE id_destinataire= :id AND id_expediteur=Users.idInscrit ORDER BY date DESC';  
		$this->messn = Controller_Template::$db->prepare($mes);
				
		$meslu= 'SELECT titre, date, message, Users.pseudo as expediteur 
				FROM messages, Users
				WHERE id_destinataire= :id AND id_expediteur=Users.idInscrit AND messages.id = :idMess'; 
		$this->mess = Controller_Template::$db->prepare($meslu);
		
		$getid = 'SELECT id FROM Users Where pseudo = :pseudo';
		$this->getId = Controller_Template::$db->prepare($getid);
		
		$infoInscritById = 'SELECT nom, prenom, pseudo,mdp, dateNaissance, photo FROM Users WHERE email = :email';
		$this->selectByIde = Controller_Template::$db->prepare($infoInscritById);
		
		$modifProfil = 'UPDATE Users SET nom = :nom, prenom = :prenom, pseudo = :pseudo, password= :mdp, dateNaissance =:date, photo= :avatar 
		                  WHERE id = :id';
		$this->update = Controller_Template::$db->prepare($modifProfil);
		
		$infoAll = 'SELECT id, nom, prenom, pseudo, password,avatar FROM Users WHERE anneeNaiss >= :anneD AND anneeNaiss <= :anneF';
		$this->selectAllP = Controller_Template::$db->prepare($infoAll);
		
		$desinscription = 'DELETE FROM Users WHERE id = :id';
		$this->delete = Controller_Template::$db->prepare($desinscription);		
		
		$infoInscrits = 'SELECT id, nom, prenom, pseudo, password,dateNaissance, avatar FROM Users WHERE id = :id';
		$this->selectAll = Controller_Template::$db->prepare($infoInscrits);
		
		$this->getAncienMdp = Controller_Template::$db->prepare('SELECT password FROM Users WHERE id = ?');
		
		$this->modifMdp = Controller_Template::$db->prepare('UPDATE Users SET password= :mdp WHERE id = :id');
		
		$this->connexion = Controller_Template::$db->prepare('SELECT login, nom, prenom, dateNaissance, avatar FROM Users WHERE login = :pseudo AND password = :mdp');
		
		$this->getIns = Controller_Template::$db->prepare('SELECT id, pseudo FROM Users');
		
		$this->sele = Controller_Template::$db->prepare('SELECT Users.pseudo as nom_destinataire, Users.id as id_destinataire FROM Users WHERE id <> :id ORDER BY Users.pseudo ASC');
	
		$this->insertMes = Controller_Template::$db->prepare('INSERT INTO messages(id_expediteur, id_destinataire, date , titre, message) VALUES(:idEx, :idDes,  :date,:titre, :mess)'); 
		
		$this->supMes = Controller_Template::$db->prepare('DELETE FROM messages WHERE id_destinataire= :id AND id= :idMess');
	
	}
	
	public function supprimeMess($id, $idMess){
		$this->supMes->execute(array(':id' => $id, 'idMess'=>$idMess));
		return $this->supMes;
	} 

	public function afficheProf($id){
		$this->selectByIde->execute(array(':id' => $id));
		return $this->selectByIde->fetch();
	} 	
	
	public function insertMess ($idEx, $idDes, $date, $titre, $mess){
		$this->insertMes->execute(array(':idEx'=> $idEx, ':idDes'=>$idDes, ':date'=> $date, ':titre'=> $titre, ':mess' => $mess));
		return $this->insertMes;
	}
	
	public function seleP($id){
		$this->sele->execute(array(':id' => $id));
		return $this->sele;
	}
	
	public function mess($id){
		$this->messn->execute(array(':id' => $id));
		return $this->messn;
	}
	
	public function meslu($id, $idMess){
		$this->mess->execute(array(':id' => $id, 'idMess' => $idMess));
		return $this->mess;
	}
        
	
	public function desinscription($id){
		$this->delete->execute(array(':id' => $id));
		return $this->delete->rowCount();
	}
	
	public function info($id){
		$this->selectAll->execute(array(':id' => $id));
		return $this->selectAll->fetch();
	}
	
	public function infoP($anneD,$anneF){
		$this->selectAllP->execute(array(':anneD' => $anneD, ':anneF' => $anneF));
		return $this->selectAllP;
	}
	
	public function inscription($nom=NULL, $prenom=NULL, $pseudo, $mdp, $email, $date=NULL, $photo=null) {
		$this->insert->execute(
		array(':nom' => $nom, ':prenom' => $prenom, ':pseudo' => $pseudo, ':mdp' => $mdp, ':email' => $email, ':date' => $date, ':photo'=>$photo));
		return $this->insert->rowCount();
            }
	
	public function modifProfil($nom, $prenom, $pseudo, $mdp, $jourNaiss, $moisNaiss, $anneeNaiss, $loisir, $info, $avatar,$sexe,$situationA, $id) {
		$this->update->execute(
		array(':nom' => $nom, ':prenom' => $prenom, ':pseudo' => $pseudo, ':mdp'=>$mdp, ':jourNaiss' => $jourNaiss, ':moisNaiss' => $moisNaiss, ':anneeNaiss' => $anneeNaiss,':sexe'=>$sexe, ':situationAmour'=> $situationA,':avatar'=> $avatar ,':loisir' => $loisir, ':id' => $id));
		return $this->update->rowCount();
	}
	
	public function verifLogInsc($log){
		$row1=$this->verifLog->execute(array(':pseudo'=> $log));
		if(empty($row1)){
			return 1;
		}
		else{
			return 0;
		}
	}
	
	public function getId($pseudo){
		$row=$this->getId-> execute(array(':pseudo' => $pseudo));
		return $this->getId->fetch();
	}
	
	public function getMdp($id) {
		$this->getAncienMdp->execute(array($id));
		$row = $this->getAncienMdp->fetchAll();
		if(empty($row[0])){
			return array();
		}
		else{
			return $row[0];
		}
	}
	
	public function connexion($pseudo, $mdp) {
		$this->connexion->execute(array(':pseudo' => $pseudo, ':mdp' => $mdp));
		return $this->connexion->fetchAll();
	}
	
	public function upload($index,$destination,$maxsize=FALSE,$extensions=FALSE){
		   //Test1: fichier correctement uploadé
			 if (!isset($_FILES[$index]) OR $_FILES[$index]['error'] > 0){ echo " non uploader "; return FALSE;}
		   //Test2: taille limite
			 if ($maxsize !== FALSE AND $_FILES[$index]['size'] > $maxsize){ echo " taille trop gros"; return FALSE;}
		   //Test3: extension
			 $ext = substr(strrchr($_FILES[$index]['name'],'.'),1);
			 if ($extensions !== FALSE AND !in_array($ext,$extensions)){ echo " extension impossible"; return FALSE; }
		   //Déplacement
			 return move_uploaded_file($_FILES[$index]['tmp_name'],$destination);
	}
}
?>

